home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / Miro_Downloader.exe / proxyfind.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-11-12  |  2.2 KB  |  75 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. '''proxyfind.py.  Get proxy info from windows.'''
  5. from ctypes import Structure, byref, windll
  6. from ctypes.wintypes import LPWSTR, BOOL
  7. import logging
  8. import re
  9.  
  10. class WINHTTP_CURRENT_USER_IE_PROXY_CONFIG(Structure):
  11.     _fields_ = [
  12.         ('fAutoDetect', BOOL),
  13.         ('lpszAutoConfigUrl', LPWSTR),
  14.         ('lpszProxy', LPWSTR),
  15.         ('lpszProxyBypass', LPWSTR)]
  16.  
  17.  
  18. class ProxyInfo:
  19.     
  20.     def __init__(self):
  21.         self.active = False
  22.         self.host = None
  23.         self.port = None
  24.         self.ignore_hosts = []
  25.  
  26.  
  27.  
  28. def get_proxy_info():
  29.     proxy_info = ProxyInfo()
  30.     ie_proxy_info = WINHTTP_CURRENT_USER_IE_PROXY_CONFIG()
  31.     rv = windll.winhttp.WinHttpGetIEProxyConfigForCurrentUser(byref(ie_proxy_info))
  32.     if not rv or ie_proxy_info.lpszProxy is None:
  33.         return proxy_info
  34.     
  35.     (proxy_info.host, proxy_info.port) = parse_host_and_port(ie_proxy_info.lpszProxy)
  36.     if ie_proxy_info.lpszProxyBypass is not None:
  37.         proxy_info.ignore_hosts = re.split('[;\\s]*', ie_proxy_info.lpszProxyBypass)
  38.     
  39.     return proxy_info
  40.  
  41.  
  42. def parse_host_and_port(windows_proxy_string):
  43.     for proxy in re.split('[;\\s]*', windows_proxy_string):
  44.         original_string = proxy
  45.         if proxy.startswith('http='):
  46.             proxy = proxy[len('http='):]
  47.         elif '=' in proxy or proxy == '':
  48.             continue
  49.         
  50.         if '://' in proxy:
  51.             if proxy.startswith('http://'):
  52.                 proxy = proxy[len('http://'):]
  53.             else:
  54.                 logging.warn('unsupported proxy scheme: %s' % original_string)
  55.         
  56.         if ':' in proxy:
  57.             (proxy, port) = proxy.split(':')
  58.             
  59.             try:
  60.                 port = int(port)
  61.             except ValueError:
  62.                 logging.warn('bad proxy port: %s' % original_string)
  63.                 continue
  64.             except:
  65.                 None<EXCEPTION MATCH>ValueError
  66.             
  67.  
  68.         None<EXCEPTION MATCH>ValueError
  69.         port = 80
  70.         return (proxy, port)
  71.     
  72.     logging.warn("couldn't find proxy: %s" % windows_proxy_string)
  73.     return (None, None)
  74.  
  75.